package com.hcb.app911;

import com.hcb.dto.ContactsDTO;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AddContactActivity extends Activity implements OnClickListener {
	EditText edusername, edphonenumber;
	Button btnAddcontacts;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_contacts);
		edusername = (EditText) findViewById(R.id.edusername);
		edphonenumber = (EditText) findViewById(R.id.edphonenumber);
		btnAddcontacts = (Button) findViewById(R.id.btnAddcontacts);
		btnAddcontacts.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnAddcontacts:
			String name = edusername.getText().toString();
			String phoneNumber = edphonenumber.getText().toString();

			if (!name.equalsIgnoreCase("") && !phoneNumber.equalsIgnoreCase("")) {
				if (isValidInput(phoneNumber)) {
					ContactsDTO dto = new ContactsDTO();
					dto.setName(name);
					dto.setNumber(phoneNumber);
					Intent intent = new Intent();
					Bundle bundle = new Bundle();
					bundle.putSerializable("myobj", dto);
					intent.putExtras(bundle);
					setResult(Activity.RESULT_OK, intent);
					finish();
				}
			}
			break;

		default:
			break;
		}

	}

	public boolean isValidInput(String phone) {

		// get 3 number first.
		String numberstring = phone.substring(0, 3);
		if (!numberstring.equals("234")) {
			new AlertDialog.Builder(this)
					.setTitle("Error!")
					.setMessage(
							"This phone number invalid, please input phone begin 234.")
					.setPositiveButton(android.R.string.yes,
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,
										int which) {
									// continue with delete
								}
							})
					/*
					 * .setNegativeButton(android.R.string.no, new
					 * DialogInterface.OnClickListener() { public void
					 * onClick(DialogInterface dialog, int which) { // do
					 * nothing } })
					 */
					.setIcon(android.R.drawable.ic_dialog_alert).show();
			return false;
		}
		return true;
	}
}
